package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.api.CommonFileApi;
import com.common.utils.StringUtils;
import com.project.domain.bo.BizUnitAptitudeBo;
import com.project.domain.entity.BizUnitAptitude;
import com.project.domain.entity.SysUploadFile;
import com.project.mapper.UnitAptitudeMapper;
import com.project.mapper.UpLoadFileMapper;
import com.project.service.IUnitAptitudeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class UnitAptitudeServiceImpl extends ServiceImpl<UnitAptitudeMapper, BizUnitAptitude> implements IUnitAptitudeService {

    @Autowired
    private UnitAptitudeMapper baseMapper;

    @Autowired
    private UpLoadFileMapper fileMapper;

    @Autowired
    private CommonFileApi fileApi;

    @Override
    public Boolean addUnitAptitude(BizUnitAptitude unitAptitude) {
        return this.save(unitAptitude);
    }

    @Override
    public Boolean updateUnitAptitude(BizUnitAptitude unitAptitude) {
        return this.updateById(unitAptitude);
    }

    @Override
    public Boolean delUnitAptitude(String[] ids) {
        List<BizUnitAptitude> bizUnitAptitudes = this.listByIds(Arrays.asList(ids));
        List<String> fileList = new ArrayList();
        //删除记录所携带的文件
        bizUnitAptitudes.forEach(e -> {
            String fileIds = e.getLicence();
            if (StringUtils.isNotEmpty(fileIds)) {
                String[] split = fileIds.split(",");
                fileList.addAll(Arrays.asList(split));
            }
        });
        for (String s : fileList) {
            try {
                fileApi.delFile(s);
            } catch (Exception e) {
                log.error("供方单位模块删除文件失败,文件id:"+s);
            }
        }

        return this.removeByIds(Arrays.asList(ids));
    }

    @Override
    public List<BizUnitAptitude> getUnitAptitudeList(BizUnitAptitudeBo bo) {
        LambdaQueryWrapper<BizUnitAptitude> lqw = buildQueryWrapper(bo);
        lqw.orderBy(true,false,BizUnitAptitude::getLastEditDate);
        List<BizUnitAptitude> list = this.list(lqw);
        return list;
    }

    @Override
    public List<HashMap<String, Integer>> listStatistics(BizUnitAptitudeBo bo) {
        LambdaQueryWrapper<BizUnitAptitude> lqw = buildQueryWrapper(bo);
        lqw.groupBy(true, BizUnitAptitude::getValidType);
        return baseMapper.listStatistics(lqw);
    }

    LambdaQueryWrapper<BizUnitAptitude>  buildQueryWrapper(BizUnitAptitudeBo bo){
        LambdaQueryWrapper<BizUnitAptitude> lqw = new LambdaQueryWrapper();
        lqw.like(StringUtils.isNotEmpty(bo.getName()), BizUnitAptitude::getName, bo.getName());
        lqw.like(StringUtils.isNotEmpty(bo.getCode()), BizUnitAptitude::getCode, bo.getCode());
        lqw.like(StringUtils.isNotEmpty(bo.getContacts()), BizUnitAptitude::getContacts, bo.getContacts());
        lqw.like(StringUtils.isNotEmpty(bo.getTelephone()), BizUnitAptitude::getTelephone, bo.getTelephone());
        lqw.in(StringUtils.isNotEmpty(bo.getBuyerType()), BizUnitAptitude::getBuyerType, bo.getBuyerType());
        lqw.in(StringUtils.isNotEmpty(bo.getProvince()), BizUnitAptitude::getProvince, bo.getProvince());
        //证书状态多值
        lqw.eq(StringUtils.isNotEmpty(bo.getValidType()), BizUnitAptitude::getValidType, bo.getValidType());
        lqw.in(StringUtils.isNotEmpty(bo.getValidTypeArr()), BizUnitAptitude::getValidType, bo.getValidTypeArr());
        return lqw;
    }

    @Override
    public BizUnitAptitude getUnitAptitudeIndex(String id) {
        BizUnitAptitude unitAptit = this.getById(id);
        //附件
        String licence = unitAptit.getLicence();
        //附件
        List<Map<String, String>> fileList = new ArrayList<>();
        if (StringUtils.hasText(licence)) {
            String[] split = licence.split(",");
            for (String s : split) {
                SysUploadFile uploadFile = fileMapper.selectById(s);
                if (uploadFile != null) {
                    Map<String, String> map = new HashMap<>();
                    map.put("id", s);
                    map.put("name", uploadFile.getUploadfilename());
                    fileList.add(map);
                }
            }

            unitAptit.setFileList(fileList);
        }
        return unitAptit;
    }
}
